package com.example.keshe0337.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.keshe0337.R;
import com.example.keshe0337.entity.ParkingSpace;
import com.example.keshe0337.entity.Rent;
import com.example.keshe0337.utils.TimeConverters;

import java.util.List;

public class RentAdapter extends ArrayAdapter<Rent> {
    private final Context context;
    private final List<Rent> rents;

    public RentAdapter(Context context, List<Rent> rents) {
        super(context, 0, rents);
        this.context = context;
        this.rents = rents;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_rent_item, parent, false);
        }

        Rent rent = rents.get(position); //若当前停车位信息为空直接跳过封
        if (rent == null) {
            return convertView;
        }
        TextView parkingNumberText = convertView.findViewById(R.id.parkingNumber);
        TextView startTimeText = convertView.findViewById(R.id.start_time);
        TextView totalTimeText = convertView.findViewById(R.id.total_time);

        parkingNumberText.setText(rent.getSpaceNum());
        startTimeText.setText(rent.getStartTime());

        Double calculate = TimeConverters.calculate(TimeConverters.parseToLong(rent.getStartTime()), TimeConverters.getNewTime());
        String total = calculate +"小时";
        totalTimeText.setText(total);

        return convertView;
    }

    public void update(List<Rent> newData) {
        clear();           // 清空旧数据
        addAll(newData);   // 添加新数据
    }

}
